﻿using Code.Services.Admins;
using Code.Services.Admins.Dto;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Logging;
using NLog;
using Code.Services.Email;
using System;
using System.Text;

namespace CodeAPP.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize("SysManage")]
    public class AdminController : ControllerBase
    {
        private readonly IAdminService adminService;
        private readonly ILogger<AdminController> logger;
        private readonly IEmailService emailService1;
        private readonly IEmailService emailService2;

        public IEmailService  EmailService4 { get; set; }

        public AdminController(
            IAdminService adminService,
            ILogger<AdminController> logger,
            IEmailService emailService1,
            IEmailService emailService2
            )
        {
            this.adminService = adminService;
            this.logger = logger;
            this.emailService1 = emailService1;
            this.emailService2 = emailService2;
        }

        [HttpGet("/admin/ditest")]
        public IActionResult DiTest()
        {
            return Ok(
                new {
                    code1 = emailService1.GetHashCode(),
                    code2 = emailService2.GetHashCode()}
                );
        }

        /// <summary>
        /// 方法注入
        /// </summary>
        /// <param name="emailService3"></param>
        /// <returns></returns>
        [HttpGet("/admin/MethodDI")]
        public IActionResult MethodDI([FromQuery]LoginDto dto)
        {
            return Ok(new
            {
                code4 = EmailService4.GetHashCode()
            });
        }

        [HttpGet("/getpre")]
        [AllowAnonymous]
        public IActionResult GenPre(string line)
        {
            StringBuilder str = new StringBuilder();
            int n = int.Parse(line);
            for (int i = 0; i <= n / 2; i++)
            {
                for (int j = 0; j < (n / 2 + i + 1); j++)
                {
                    if (j < (n / 2 - i))
                        str.Append(" ");                        
                    else
                        str.Append("*");
                }
                str.Append(Environment.NewLine);
            }
            for (int i = n / 2; i > 0; i--)
            {
                for (int j = 0; j < i + n / 2; j++)
                {
                    if (j <= n / 2 - i)
                        str.Append(" ");
                    else
                        str.Append("*");
                }
                str.Append(Environment.NewLine);
            }
            
            return Ok(str.ToString());
        }

        /// <summary>
        /// /特性路由
        /// </summary>
        /// <param name="loginDto"></param>
        /// <returns></returns>
        [HttpPost("/login")]
        [AllowAnonymous]
        public async Task<IActionResult> Login(LoginDto loginDto)
        {
            return Ok(await adminService.Login(loginDto));
        }

        /// <summary>
        /// /特性路由
        /// </summary>
        /// <param name="loginDto"></param>
        /// <returns></returns>
        [HttpGet("/adminList")]
        [Authorize]
        public async Task<IActionResult> adminList(int PageIndex, int PageSize = 10)
        {
            return new JsonResult(adminService.AdminList(PageIndex, PageSize));
        }

        [HttpPost("/admin/create")]
        public IActionResult Create(AdminCreateOrUpdateDto dto)
        {
            return Ok(adminService.Create(dto));
        }

        [HttpGet("/admin/edit")]
        public IActionResult Edit(int id)
        {
            return Ok(adminService.Edit(id));
        }

        [HttpPost("/admin/update")]
        public IActionResult Update(AdminCreateOrUpdateDto dto)
        {
            return Ok(adminService.Update(dto));
        }

        [HttpGet("/admin/delete")]
        public IActionResult Delete(int id)
        {
            return Ok(adminService.Delete(id));
        }

        [HttpPost("/admin/bulkdelete")]
        public IActionResult Delete(int[] ids)
        {
            return Ok(adminService.Delete(ids));
        }
    }
}